package com.yaodian100.core.common.utils.servlet;

/**
 * @author jinwei.lin
 *
 */
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;

/**
 * Servlet implementation class ServerValueInit
 */
public class ServerValueInit extends HttpServlet {
	private final Log log = LogFactory.getLog(ServerValueInit.class);
	private static final long serialVersionUID = 1L;
	private String urlPath = "http://localhost/static/server/.server.prop";
	private String seoPath = "http://localhost/static/server/frontpage.properties";

	/**
	 * Default constructor.
	 */
	public ServerValueInit() {
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		ServerValue.getInstance().setDefault(urlPath, seoPath);
		response.getWriter().write("path:" + request.getRequestURL().toString() + "\n");
		response.getWriter().write("done:" + DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", new Date()));
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		ServerValue.getInstance().setDefault(urlPath, seoPath);
		response.getWriter().write("path:" + request.getRequestURL().toString() + "\n");
		response.getWriter().write("done:" + DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", new Date()));
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		if (StringUtils.isNotBlank(config.getInitParameter("urlPath"))) {
			urlPath = config.getInitParameter("urlPath");
		}
		if (StringUtils.isNotBlank(config.getInitParameter("seoPath"))) {
			seoPath = config.getInitParameter("seoPath");
		}
		ServerValue.getInstance().setDefault(urlPath, seoPath);
	}
}